<template>
<!-- pages/common/signin/signin.wxml -->
<view class="a">
    <view class="b">
        <view>我的积分：{{integral}}</view>
        <view>
            <navigator url="/pages/ucenter/integral/integral">积分记录></navigator>
        </view>
    </view>
    <button class="default" size="default" :type="form.isSignIn ? '' : 'primary'" @tap="signin">
        {{form.title}}
    </button>
</view>
</template>

<script>
// pages/common/signin/signin.js
let app = getApp();

export default {
  data() {
    return {
      form: {
        isSignIn: false,
        title: "签到"
      },
      a: {
        "message": "签到成功",
        "user": "1"
      },
      b: {
        "category": "1",
        "origin": "签到",
        "user": "1",
        "value": 2
      },
      userInfo: {},
      integral: 0,
      status: false
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onGetUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onGetUserInfo();
    uni.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  methods: {
    signin() {
      let form = this.form;
      let user = this.userInfo.id;
      let a = this.a;
      let b = this.b;

      if (form.isSignIn) {
        uni.showToast({
          title: '不可重复签到',
          icon: "none"
        });
      } else {
        a.user = user;
        b.user = user;
        uni.showLoading({
          title: '签到中'
        });
        uni.request({
          url: `${app.globalData.baseUrl}/integral`,
          complete: res => {
            uni.hideLoading();
          },
          data: b,
          fail: res => {
            uni.hideLoading();
            uni.showToast({
              title: '签到失败',
              icon: "none",
              duration: 4000
            });
          },
          method: "POST",
          success: result => {
            uni.request({
              url: `${app.globalData.baseUrl}/message/save`,
              data: a,
              method: "POST"
            });
            form.isSignIn = true;
            form.title = "已签到";
            this.setData({
              form
            });
            uni.startPullDownRefresh();
          }
        });
      }
    },

    getStatus(user) {
      let _this = this;

      uni.request({
        url: `${app.globalData.baseUrl}/integral/states/${user}`,
        success: result => {
          let form = _this.form;
          console.log(form);
          console.log(result);

          if (result.data.dataMap.data.id) {
            form.isSignIn = true;
            form.title = "已签到";

            _this.setData({
              form
            });
          } else {
            form.isSignIn = false;
            form.title = "签到";

            _this.setData({
              form
            });
          }
        }
      });
    },

    getIntegral(user) {
      uni.request({
        url: `${app.globalData.baseUrl}/integral/sum/${user}`,
        complete: res => {},
        fail: res => {},
        success: result => {
          this.setData({
            integral: result.data.dataMap.data
          });
        }
      });
    },

    onGetUserInfo() {
      let openid = uni.getStorageSync('openid');

      let _this = this;

      if (!openid) {
        uni.navigateBack({
          complete: res => {
            uni.showToast({
              title: '请登录',
              icon: "none"
            });
          }
        });
      } else {
        uni.showLoading({
          title: '加载中'
        });
        uni.request({
          url: `${app.globalData.baseUrl}/user/selectIsUser/${openid}`,
          complete: res => {
            uni.hideLoading();
          },
          success: result => {
            _this.setData({
              userInfo: result.data.dataMap.data
            });

            _this.getIntegral(result.data.dataMap.data.id);

            _this.getStatus(result.data.dataMap.data.id);
          }
        });
      }
    },

    onShareTimeline() {}

  }
};
</script>
<style>
/* pages/common/signin/signin.wxss */
.default {
    width: 400rpx;
    height: 400rpx;
    border-radius: 50%;
    text-align: center;
    line-height: 400rpx;
    margin-top: 30%;
    font-size: xx-large;
}

.default::after {
    border: none;
}

.a {
    width: 96%;
    margin: 2%;
}

.b {
    width: 100%;
    height: 100rpx;
    line-height: 100rpx;
    background: white;
    font-size: 40rpx;
    display: flex;
    justify-content: space-between;
}

.b view {
    margin: 0rpx 10rpx;
}

</style>